package com.topscomm.pm.quartz;

import com.topscomm.basic.BasicEntity;
import com.topscomm.pm.pojo.PmReceiptApplyEntity;
import com.topscomm.pm.service.impl.PmRecBillService;
import com.topscomm.pm.service.impl.PmReceiptApplyService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.tap.quartz.TapJob;
import org.quartz.JobExecutionContext;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: wushenghong
 * @date: 2020/7/24
 * @description: 根据收款信息自动更新收据的回款金额
 */
public class PmReceiptReturnMoneyJob extends TapJob {

    @Override
    public void run(JobExecutionContext jobExecutionContext) {
        PmRecBillService pmRecBillService = (PmRecBillService) SpringUtil.getBean("pmRecBillService");
        PmReceiptApplyService pmReceiptApplyService = (PmReceiptApplyService) SpringUtil.getBean("pmReceiptApplyService");
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle("select projectid,sum(NVL(money,0)) as totalreturnmoney from PmRecBill where state = 9 group by PmRecBill.projectid");
        List<Map<String, Object>> totalReturnMoneyOfProjectList = pmRecBillService.selectExecute(sql.toString());
        sql.clear();
        if (totalReturnMoneyOfProjectList.size() > 0) {
            for (Map<String, Object> totalReturnMoneyOfProjectMap : totalReturnMoneyOfProjectList) {
                BigDecimal totalReturnMoney = ConvertUtil.convertToBigDecimal(totalReturnMoneyOfProjectMap.get("totalreturnmoney"));
                long projectId = ConvertUtil.convertToLong(totalReturnMoneyOfProjectMap.get("projectid"));
                // 查询当前项目的所有收据(新增、重开,且没作废的)记录，按收据日期排序
                sql.appendSingle(" projectid = {0} and state = 9 and category != '2' and iscancel = 0 order by maildate asc", projectId);
                List<PmReceiptApplyEntity> pmReceiptApplyEntityList = pmReceiptApplyService.queryByWhere(sql.toString());
                sql.clear();
                List<BasicEntity> pmReceiptApplyEntities = new ArrayList<BasicEntity>();
                if (pmReceiptApplyEntityList.size() > 0 && totalReturnMoney.compareTo(BigDecimal.ZERO) > 0) {
                    int flag = 0;
                    for (PmReceiptApplyEntity pmReceiptApplyEntity : pmReceiptApplyEntityList) {
                        if (totalReturnMoney.subtract(pmReceiptApplyEntity.getReceiptmoney()).compareTo(BigDecimal.ZERO) > 0) {
                            pmReceiptApplyEntity.setReturnmoney(pmReceiptApplyEntity.getReceiptmoney());
                            pmReceiptApplyEntities.add(pmReceiptApplyEntity);
                            totalReturnMoney = totalReturnMoney.subtract(pmReceiptApplyEntity.getReceiptmoney());
                            flag++;
                        } else {
                            pmReceiptApplyEntity.setReturnmoney(totalReturnMoney);
                            pmReceiptApplyEntities.add(pmReceiptApplyEntity);
                            flag++;
                            break;
                        }
                    }
                    // 考虑作废收据回退后，回款脏数据清理
                    for (int i = flag; i < pmReceiptApplyEntityList.size(); i++) {
                        PmReceiptApplyEntity pmReceiptApplyEntity = pmReceiptApplyEntityList.get(i);
                        pmReceiptApplyEntity.setReturnmoney(BigDecimal.ZERO);
                        pmReceiptApplyEntities.add(pmReceiptApplyEntity);
                    }
                    pmReceiptApplyService.updateBatch(pmReceiptApplyEntities);
                }
            }
        }
    }
}
